package com.sharemob.tinchat.base;

import java.util.ArrayList;

import com.sharemob.tinchat.R;
import com.sharemob.tinchat.lib.common.SMGlobal;


import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class ListAdapterForNearby extends BaseAdapter {


	ArrayList<FriendItem> list = new ArrayList<FriendItem>();

	@Override
	public int getCount()
	{
		return list.size();
	}

	@Override
	public Object getItem(int position)
	{
		return list.get(position);
	}

	@Override
	public long getItemId(int position)
	{
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent)
	{
		ViewHolder viewHolder = null;
		if (convertView == null)
		{
			viewHolder = new ViewHolder();

			convertView = View.inflate(parent.getContext(),R.layout.layout_listview_nearby, null);
			findViews(viewHolder, convertView);
			
			convertView.setTag(viewHolder);
		}
		else
		{
			
			viewHolder = (ViewHolder) convertView.getTag();
		}
		setView(viewHolder, position);
		return convertView;
	}

	private void setView(final ViewHolder viewHolder, int position)
	{
		FriendItem item = list.get(position);
		viewHolder.header_icon.setImageResource(item.getHeader_resid());
		viewHolder.city.setText(item.getCity());
		viewHolder.nickname.setText(item.getNickname());
		viewHolder.place.setText(item.getPlace());	
		if(item.getSex()==0){
			viewHolder.sex_age.setText(String.valueOf(item.getAge()));
			viewHolder.sex_age.setBackgroundResource(R.drawable.style_bg_sex_male);
			viewHolder.sex_age.setCompoundDrawables(SMGlobal.getInstance().getDrawableByResID(R.drawable.ic_icon_male),null, null, null);
		}else{
			viewHolder.sex_age.setText(String.valueOf(item.getAge()));
			viewHolder.sex_age.setBackgroundResource(R.drawable.style_bg_sex_female);
		}
			
	}

	public void setAdapter(ArrayList<FriendItem> list)
	{
		this.list = list;
	}

	private void findViews(ViewHolder viewHolder, View convertView)
	{
		viewHolder.header_icon = (ImageView) convertView.findViewById(R.id.user_header_icon);
		viewHolder.nickname = (TextView) convertView.findViewById(R.id.user_nickname);
		viewHolder.sex_age=(TextView)convertView.findViewById(R.id.user_sex_age);
		viewHolder.city=(TextView)convertView.findViewById(R.id.user_city);
		viewHolder.place=(TextView)convertView.findViewById(R.id.user_place);
	}

	protected final class ViewHolder
	{
		ImageView header_icon;
		TextView nickname;
		TextView sex_age;
		TextView place;
		TextView city;
		ImageView item_arrow_icon;
		
	}

}
